package com.aizhixin.lab.post.server;

import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.post.data.PostSkillData;
import com.aizhixin.lab.post.domain.PostSkillDomain;
import com.aizhixin.lab.post.entity.Post;
import com.aizhixin.lab.post.entity.PostSkill;
import com.aizhixin.lab.post.repository.PostRepository;
import com.aizhixin.lab.post.repository.PostSkillRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class PostSkillService {
    @Autowired
    private PostSkillRepository skillRepository;

    public Integer skillNum(String postId){
        return skillRepository.countByPostId(postId);
    }
    public PostSkill save(PostSkill postSkill){
        return skillRepository.save(postSkill);
    }
    public List<PostSkill> save(List<PostSkill> skillList){
        return skillRepository.save(skillList);
    }
    public Integer delete(String postId){
        return skillRepository.deleteByPostId(postId);
    }
    public List<PostSkill> findPostSkill(String postId){
        return skillRepository.findByPostIdAndDeleteFlag(postId, DataValidity.VALID.getState());
    }
    public List<PostSkillDomain> findPostSkillListDomain(String postId){
        List<PostSkillDomain> domainList=new ArrayList<>();
        List<PostSkill> postSkillList = skillRepository.findByPostIdAndDeleteFlag(postId, DataValidity.VALID.getState());
        for (PostSkill postSkill:postSkillList){
            PostSkillDomain skillDomain = new PostSkillDomain();
            BeanUtils.copyProperties(postSkill,skillDomain);
            domainList.add(skillDomain);
        }
        return domainList;
    }
    public List<PostSkillData> findPostSkillListData(String postId){
        List<PostSkillData> domainList=new ArrayList<>();
        List<PostSkill> postSkillList = skillRepository.findByPostIdAndDeleteFlag(postId, DataValidity.VALID.getState());
        for (PostSkill postSkill:postSkillList){
            PostSkillData skillDomain = new PostSkillData();
            BeanUtils.copyProperties(postSkill,skillDomain);
            domainList.add(skillDomain);
        }
        return domainList;
    }
    public List<PostSkill> findList(List<String> idList){
        return skillRepository.findAll(idList);
    }

    public PostSkill deleteSkill(String id){
        PostSkill postSkill = skillRepository.findOne(id);
        postSkill.setDeleteFlag(DataValidity.INVALID.getState());
        return skillRepository.save(postSkill);
    }
    public String saveDomain(PostSkillDomain domain){
        PostSkill postSkill = new PostSkill();
        domain.setId(UUID.randomUUID().toString());
        BeanUtils.copyProperties(domain,postSkill);
        skillRepository.save(postSkill);
        return "OK";
    }
    public String updateDomain(PostSkillDomain domain){
        PostSkill postSkill = skillRepository.findOne(domain.getId());
        BeanUtils.copyProperties(domain,postSkill);
        skillRepository.save(postSkill);
        return "OK";
    }

    public PostSkillDomain findPostSkillDomain(String id){
        PostSkill postSkill = skillRepository.findOne(id);
        PostSkillDomain skillDomain = new PostSkillDomain();
        BeanUtils.copyProperties(postSkill,skillDomain);
        return skillDomain;
    }
}
